package xyz.app.domain.roles;

import lombok.*;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import xyz.app.Dao;
import xyz.app.MutableDomain;
import xyz.app.dependencies.HasDomainMembers;
import xyz.app.dependencies.HasOwners;
import xyz.app.dependencies.MemberAccessor;
import xyz.app.DaoRegistry;
import xyz.app.dependencies.OwnerEntry;
import xyz.app.Wrapper;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class DaoParent implements Dao<Parent>,
        HasDomainMembers<Parent>,
        HasOwners.IsRemovableOrphan<Parent>,
        MutableDomain {

    @Getter @Accessors(fluent = true)
    private final DaoRegistry daoRegistry;

    @Getter @Accessors(fluent = true)
    private final Collection<MemberAccessor> memberAccessors = Set.of(
            MemberAccessor.of(AbstractRole_.PARTY, Wrapper::party, Wrapper::party));

    private final Collection<OwnerEntry<?, Parent>> ownersMapping = new HashSet<>();

    public Collection<OwnerEntry<?, Parent>> ownersMapping() {
        if (ownersMapping.isEmpty()) daoRegistry
                .ownerEntry(Branch_.parent, Branch.class)
                .ifPresent(ownersMapping::add);

        return ownersMapping;
    }

    @Override
    public @NonNull Class<Parent> domainClass() {
        return Parent.class;
    }
}